<?xml version="1.0"?>
<OnTheFlyInstruction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <InstanceId>FieldRefSample</InstanceId>
  <CustomAppConfig />
  <Tags>
    <string>SharePoint</string>
  </Tags>
  <AdditionalAssemblies />
  <Code><![CDATA[// Gets a few fields and create a FieldRef collection
// used to create the fieldRef sectino of a contenttype definition

// v 1.0 - 2013_07_02 - rev 0

using System;using xSolon.Instructions.DTO;
using System.Linq;using System.Data;
using xSolon.Instructions;using System.Collections.Generic;
using Microsoft.SharePoint;using System.Xml;
using xSolon.Instructions.SP;using System.Text;

public class FieldRefSample : AbstractInstruction
{

	public override void Run()
	{

		using (var site = new SPSite("http://localhost"))
		using (var web = site.OpenWeb())
		{
			
			// Simple serialization of cTypes
			// visible in the 'Data Table' tab
			
			// Sample filter
			var cols = web.AvailableFields.GetSerializedList()
				.Where(i => i["Group"] == "group")
				.Where(i => i["InternalName"].StartsWith("prefix"))
				.ToList();
				
			// Create Table -----------------------------------------
			var table = cols.ListToTable();
				
			// Remove uncessary columns
			table.Columns.OfType<DataColumn>().Where(i => i.ColumnName != "SchemaXml")
				.ToList().ForEach(i => table.Columns.Remove(i));
				
			table.AcceptChanges();
			
			ResultTable = table;
			// ------------------------------------------------------
			
			Response = "";
			// Create FieldRefs -------------------------------------
			cols.ForEach(i => Response += string.Format("<FieldRef ID=\"{0}\" Name=\"{1}\" />", 
				"{" + i["Id"].ToLower() + "}",i["InternalName"]));
			// ------------------------------------------------------
			
			// Delete this fields -----------------------------------
			/*
			cols.ForEach(i =>{
				NotifyInformation("Deleting {0}",i["InternalName"]);
				web.Fields.Delete(i["InternalName"]);
			});
			web.Update();
			*/
			// ------------------------------------------------------
			
		}

	}

	
}]]></Code>
</OnTheFlyInstruction>